package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.entity.SysMenu;
import com.medical.lsq.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController  {
    @Autowired
    private MenuService menuService;
    /**
     *分页获取权限列表
     *@param currentPage
     * @param limit
     * 	 @param request
     * 	  @param model
     * 	  @return
     */
    @RequestMapping ("/getMenuList")
    public String getMenuList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String menuName = request.getParameter("context");
        currentPage = currentPage !=null ? currentPage : 1;
        limit = limit !=null ? limit :10;
        PageInfo<SysMenu>pageInfo = menuService.getMenuList(menuName,currentPage,limit);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("context",menuName);
        System.out.println("context"+menuName);
        return "/menu/menu_list";
    }

    @RequestMapping("/openAdd")
    public String openApp(Model model) {return "/menu/menu_add";}

    @RequestMapping("/openEdit")
    public String openEdit(String id,Model model) {
        SysMenu menu =menuService.getMenuById(id);

        model.addAttribute("menu",menu);
        return "/menu/menu_list";
    }
    @RequestMapping("/add")
    public String add(SysMenu menu, HttpServletRequest request, Model model) {
        menuService.add(menu);
        return getMenuList(1, 10, request, model);
    }
    @RequestMapping("edit")
    public String edit(SysMenu menu, HttpServletRequest request, Model model) {
        menuService.edit(menu);
        return getMenuList(1, 10, request, model);
    }
    @RequestMapping("delete")
    public String delete(String id, HttpServletRequest request, Model model) {
        menuService.delete(id);
        return getMenuList(1, 10, request,model);
    }
    @ResponseBody
    @RequestMapping("getMenuTreeList")
    public Object getMenuTreeList() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("menuList", menuService.getMenuTree(""));
        return map;
    }


}
